<?php


namespace app\controller\admin;

use app\model\AdminMerchantNet;
use app\util\ReturnCode;
use think\facade\Db;
use think\Response;

class MerchantNet extends Base
{

    /**
     * @notes: 商户配置信息列表
     * @return Response
     * @throws \think\db\exception\DbException
     * @author: speed
     */
    public function index(): Response {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $keywords = $this->request->get('keywords', '');

        $obj = (new AdminMerchantNet());

        if ($keywords) {
            $obj = $obj->whereLike('order_no', "%{$keywords}%");
        }
        $listObj = $obj->with(['school'])
            ->append(['pay_type_text','merchant_type_text'])
            ->order(['create_time' => 'DESC'])
            ->paginate(['page' => $start, 'list_rows' => $limit])->toArray();

        $listInfo = $listObj['data'];

        return $this->buildSuccess([
            'list'  => $listInfo,
            'count' => $listObj['total']
        ]);
    }

    /**
     * 新增商户
     * @return Response
     */
    public function add(): Response {
        $data = [
            'school_id'       => input('post.school_id/d'),
            'pay_type'      => input('post.pay_type/d', ''),
            'merchant_type'       => input('post.merchant_type/d'),
            'merchant_no'  => input('post.merchant_no/s', ''),
            'appid'        => input('post.appid/s'),
            'secret'  => input('post.secret/s'),
            'private_key'       => input('post.private_key/s', ''),
            'public_key'       => input('post.public_key/s', ''),
            'notify_url'       => input('post.notify_url/s', ''),
        ];
        $exists = AdminMerchantNet::where([
            'school_id' => $data['school_id'],
            'merchant_type' => $data['merchant_type']
        ])->findOrEmpty();
        if (!$exists->isEmpty()) {
            return $this->buildFailed(ReturnCode::DATA_EXISTS, "当前学校已配置对应类型的支付商户，无法重复添加");
        }

        Db::startTrans();
        $res = AdminMerchantNet::create($data);
        if ($res === false) {
            Db::rollback();
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        Db::commit();
        return $this->buildSuccess();
    }

    /**
     * @notes: 编辑商户
     * @return Response
     * @author: speed
     */
    public function edit(): Response {
        $postData = $this->request->post();
        $data = [
            'school_id'       => input('post.school_id/d'),
            'pay_type'      => input('post.pay_type/d', ''),
            'merchant_type'       => input('post.merchant_type/d'),
            'merchant_no'  => input('post.merchant_no/s', ''),
            'appid'        => input('post.appid/s'),
            'secret'  => input('post.secret/s'),
            'private_key'       => input('post.private_key/s', ''),
            'public_key'       => input('post.public_key/s', ''),
            'notify_url'       => input('post.notify_url/s', ''),
        ];
        Db::startTrans();
        $res = AdminMerchantNet::update($data, ['id' => $postData['id']]);
        if ($res === false) {
            Db::rollback();
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR);
        }
        Db::commit();
        return $this->buildSuccess();
    }

    /**
     * @notes: 删除商户
     * @return Response
     * @author: speed
     */
    public function del(): Response {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        (new AdminMerchantNet())->whereIn('id', $id)->delete();
        return $this->buildSuccess();
    }
}